package com.dy.drp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dy.drp.dto.SaleShortTimeDto;
import com.dy.drp.entity.Cargo;
import com.dy.drp.entity.Saleshorttime;
import com.dy.drp.service.ICargoService;
import com.dy.drp.service.ISaleshorttimeService;
import com.dy.drp.vo.SaleinfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dy
 * @since 2022-04-01
 */
@RestController
@RequestMapping("/saleshorttime")
@Api(tags = "销售出库接口")
public class SaleshorttimeController {
    @Autowired
    private ISaleshorttimeService saleshorttimeService;
    @Autowired
    private ICargoService cargoService;

    @GetMapping("/insertSaleCargo/{cargoName}/{cargoFactory}")
    @ApiOperation("添加出库货物")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cargoName",value = "货物名"),
            @ApiImplicitParam(name = "cargoFactory",value = "厂家")
    })
    public String insertSaleCargo(@PathVariable("cargoName") String cargoName,
                                  @PathVariable("cargoFactory") String cargoFactory){
        QueryWrapper<Cargo> wrapper = new QueryWrapper<>();
        wrapper.eq("cargo_name",cargoName);
        wrapper.eq("cargo_factory",cargoFactory);
        Cargo one = cargoService.getOne(wrapper);
        Saleshorttime saleshorttime = new Saleshorttime(null,one.getCargoId(),0L);
        if(saleshorttimeService.save(saleshorttime)){
            return "添加成功";
        }
        return "添加失败";
    }

    @GetMapping("/updateCargoNumber/{cargoName}/{cargoFactory}/{number}")
    @ApiOperation("修改货物出库数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cargoName",value = "货物名"),
            @ApiImplicitParam(name = "cargoFactory",value = "厂家"),
            @ApiImplicitParam(name = "number",value = "出库数量")
    })
    @Transactional
    public String updateCargoNumber(@PathVariable("cargoName")String cargoName,
                                     @PathVariable("cargoFactory")String cargoFactory,
                                     @PathVariable("number")Long number){
        QueryWrapper<Cargo> wrapper = new QueryWrapper<>();
        wrapper.eq("cargo_name",cargoName);
        wrapper.eq("cargo_factory",cargoFactory);
        Cargo one = cargoService.getOne(wrapper);
        if (one.getCargoNumber() < number){
            return "修改失败";
        }
        Saleshorttime saleshorttime = new Saleshorttime(null,one.getCargoId(),number);
        QueryWrapper<Saleshorttime> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cargo_id",one.getCargoId());
        if(saleshorttimeService.update(saleshorttime,queryWrapper)){
            return "修改成功";
        }
        return "修改失败";
    }

    @GetMapping("/deleteSaleCargo/{cargoName}/{cargoFactory}")
    @ApiOperation("移除商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name="cargoName",value = "货物名"),
            @ApiImplicitParam(name = "cargoFactory",value = "厂家")
    })
    public String deleteSaleCargo(@PathVariable("cargoName")String cargoName,
                                   @PathVariable("cargoFactory")String cargoFactory){
        QueryWrapper<Cargo> wrapper = new QueryWrapper<>();
        wrapper.eq("cargo_name",cargoName);
        wrapper.eq("cargo_factory",cargoFactory);
        Cargo one = cargoService.getOne(wrapper);
        QueryWrapper<Saleshorttime> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cargo_id",one.getCargoId());
        if(saleshorttimeService.remove(queryWrapper)){
            return "移除成功";
        }
        return "移除失败";
    }

    @PostMapping ("/saleFromFactory")
    @ApiOperation("销售出库")
    public String saleFromFactory(SaleinfoVo saleinfoVo){
        if(saleshorttimeService.saleFromFactory(saleinfoVo)){
            return "出库成功";
        }
        return "出库失败";
    }

    @GetMapping("/selectAllSaleShortTime")
    @ApiOperation("获取所有临时销售货物")
    public List<SaleShortTimeDto> selectAllSaleShortTime(){
        List<SaleShortTimeDto> saleShortTimeDtos = saleshorttimeService.selectAllSaleShortTime();
        saleShortTimeDtos.stream().forEach(System.out::println);
        return saleShortTimeDtos;
    }
}
